package com.hongxun.modules.api.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hongxun.common.utils.*;
import com.hongxun.common.validator.Assert;
import com.hongxun.modules.api.annotation.AuthIgnore;
import com.hongxun.modules.api.annotation.LoginUser;
import com.hongxun.modules.api.entity.*;
import com.hongxun.modules.api.service.*;
import com.hongxun.modules.bss.entity.*;
import com.hongxun.modules.bss.service.*;
import com.hongxun.modules.eys.entity.*;
import com.hongxun.modules.eys.service.*;
import com.hongxun.modules.sys.controller.AbstractController;
import com.hongxun.modules.sys.entity.SysDeptEntity;
import com.hongxun.modules.sys.entity.SysUserEntity;
import com.hongxun.modules.sys.service.SysDeptService;
import com.hongxun.modules.sys.service.SysUserService;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Created by admin on 2018/7/16.
 */
@RestController
@RequestMapping("/rest/user")
public class LoginController extends AbstractController {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private RedisUtils redisUtil;
    @Autowired
    SysDeptService sysDeptService;
    @Autowired
    BDataDictionaryService bDataDictionaryService;
    @Autowired
    BDeviceService bDeviceService;//设备
    @Autowired
    BDeviceQuantitiesService bDeviceQuantitiesService;//设备工程量
    @Autowired
    BMaterialService bMaterialService;//材料
    @Autowired
    BMaterialQuantitiesService bMaterialQuantitiesService;//材料工程量
    @Autowired
    BCrossQuantitiesService bCrossQuantitiesService;//交垮工程量
    @Autowired
    BBudgetItemService bBudgetItemService;//预算项
    @Autowired
    BBudgetStandardService bBudgetStandardService;//预算标准
    @Autowired
    BEntryCriteriaService bEntryCriteriaService;//准入条件
    @Autowired
    BOtherCostService bOtherCostService;//其他费用模板
    @Autowired
    BOtherQuantitiesService bOtherQuantitiesService;//其他工程量
    @Autowired
    BProjectApplicationService bProjectApplicationService;//申请书
    @Autowired
    BEstimateService bEstimateService;//估算简表
    @Autowired
    BPointConfigService bPointConfigService;//点位配置表

    /**
     * 项目基本信息
     */
    @Autowired
    APBaseService apBaseService;//项目
    @Autowired
    APCostService apCostService;
    @Autowired
    APCriteriaService apCriteriaService;
    @Autowired
    APEstimateService apEstimateService;
    @Autowired
    APOtherService apOtherService;
    @Autowired
    APRangeService apRangeService;
    @Autowired
    APWireService apWireService;
    /**
     * 项目点位信息
     */
    @Autowired
    DCrossCostService dCrossCostService;
    @Autowired
    DDrawLineService dDrawLineService;
    @Autowired
    DPointService dPointService;
    /**
     * 项目设备及材料信息
     */
    @Autowired
    DDeviceOldService dDeviceOldService;
    @Autowired
    DDeviceBuyService dDeviceBuyService;
    @Autowired
    DMaterialBuyService dMaterialBuyService;
    @Autowired
    DMaterialOldService dMaterialOldService;

    /**
     * 项目工程量信息
     */
    @Autowired
    DOtherCostService dOtherCostService;
    @Autowired
    DMaterialCostService dMaterialCostService;
    @Autowired
    DWorkContentService dWorkContentService;

    @Autowired
    DPictureService dPictureService;
    /**
     * 隐患
     */
    @Autowired
    EProblemService eProblemService;
    @Autowired
    EProblemexistService eProblemexistService;
    @Autowired
    EPointService ePointService;
    @Autowired
    ECrossCostService eCrossCostService;
    @Autowired
    EDrawLineService eDrawLineService;
    @Autowired
    EPBaseService epBaseService;
    @Autowired
    EPRangeService epRangeService;
    @Autowired
    EPWireService epWireService;
    @Autowired
    EDeviceBuyService eDeviceBuyService;
    @Autowired
    EDeviceOldService eDeviceOldService;
    @Autowired
    EMaterialBuyService eMaterialBuyService;
    @Autowired
    EMaterialOldService eMaterialOldService;
    @Autowired
    EWorkContentService eWorkContentService;

    @Autowired
    DMaterialModelService dMaterialModelService;
    @Autowired
    DMaterialModelItemService dMaterialModelItemService;

    @AuthIgnore
    @RequestMapping(value = "/login")
    public R login(String username, String password) {
        Assert.isBlank(username, "用户名不能为空");
        Assert.isBlank(password, "密码不能为空");
        SysUserEntity sysUserEntity = null;
//        if (XUtils.isMobileNO(username)) {
//            sysUserEntity = sysUserService.queryByMobile(username);
//        } else {
//        }
        sysUserEntity = sysUserService.queryByUserName1(username);
        if (sysUserEntity == null) return R.error("用户不存在");
        // 账号不存在、密码错误
        if (sysUserEntity == null || !sysUserEntity.getPassword().equals(new Sha256Hash(password, sysUserEntity.getSalt()).toHex())) {
            return R.error("账号或密码不正确");
        }
        // 账号锁定
        if (sysUserEntity.getStatus() == 0) {
            return R.error("账号已被锁定,请联系管理员");
        }
        // 生成token
        String token = jwtUtils.generateToken(sysUserEntity.getUserId());
        SysDeptEntity sysDeptEntity = sysDeptService.queryObject(sysUserEntity.getDeptId());
        sysUserEntity.setDeptName(sysDeptEntity.getName());
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());
        map.put("user", sysUserEntity);
        redisUtil.set(token, sysUserEntity, jwtUtils.getExpire());
        return R.ok(map);
    }

    @RequestMapping("/data")
    public R getData(@RequestParam Map<String, Object> map) {

        List<BDataDictionaryEntity> bDataDictionaryEntities = bDataDictionaryService.queryList(map);
        List<BDeviceEntity> bDeviceEntities = bDeviceService.queryList(map);
        List<BMaterialEntity> bMaterialEntities = bMaterialService.queryList(map);
        List<BCrossQuantitiesEntity> bCrossQuantitiesEntities = bCrossQuantitiesService.queryList(map);
        List<BBudgetItemEntity> bBudgetItemEntities = bBudgetItemService.queryList(map);
        List<BBudgetStandardEntity> bBudgetStandardEntities = bBudgetStandardService.queryList(map);
        List<BEntryCriteriaEntity> bEntryCriteriaEntities = bEntryCriteriaService.queryList(map);
        List<BOtherCostEntity> bOtherCostEntities = bOtherCostService.queryList(map);
        List<BOtherQuantitiesEntity> bOtherQuantitiesEntities = bOtherQuantitiesService.queryList(map);
        List<BProjectApplicationEntity> bProjectApplicationEntities = bProjectApplicationService.queryList(map);
        List<BEstimateEntity> bEstimateEntities = bEstimateService.queryList(map);
        List<BPointConfigEntity> bPointConfigEntities = bPointConfigService.queryList(map);
        List<SysUserEntity> sysUserEntities = sysUserService.queryList(map);
        return R.ok().put("bDataDictionaryEntities", bDataDictionaryEntities)
                .put("bDeviceEntities", bDeviceEntities)
                .put("bMaterialEntities", bMaterialEntities)
                .put("bCrossQuantitiesEntities", bCrossQuantitiesEntities)
                .put("bBudgetItemEntities", bBudgetItemEntities)
                .put("bBudgetStandardEntities", bBudgetStandardEntities)
                .put("bEntryCriteriaEntities", bEntryCriteriaEntities)
                .put("bOtherCostEntities", bOtherCostEntities)
                .put("bOtherQuantitiesEntities", bOtherQuantitiesEntities)
                .put("bProjectApplicationEntities", bProjectApplicationEntities)
                .put("bEstimateEntities", bEstimateEntities)
                .put("bPointConfigEntities", bPointConfigEntities)
                .put("sysUserEntities", sysUserEntities);
    }

    @RequestMapping("/data2")
    public R getData2() {
        Map<String, Object> map = new HashMap<>();
        List<BDeviceQuantitiesEntity> bDeviceQuantitiesEntities = bDeviceQuantitiesService.queryList(map);
        return R.ok().put("bDeviceQuantitiesEntities", bDeviceQuantitiesEntities);
    }

    @RequestMapping("/data3")
    public R getData3() {
        Map<String, Object> map = new HashMap<>();
        List<BMaterialQuantitiesEntity> bMaterialQuantitiesEntities = bMaterialQuantitiesService.queryList(map);
        List<EProblemEntity> eProblemEntities = eProblemService.queryList(map);
        return R.ok()
                .put("bMaterialQuantitiesEntities", bMaterialQuantitiesEntities)
                .put("eProblemEntities", eProblemEntities)
                ;
    }


    /**
     * 更新项目（基本信息）
     *
     * @return
     */
    @RequestMapping("/projectUpdate")
    public R updateProject(@RequestParam Map<String, Object> map) {
        APBaseEntity apBaseEntity = JSON.parseObject(map.get("apBaseEntity").toString(), APBaseEntity.class);
        APBaseEntity a = apBaseService.queryObject(apBaseEntity.getGuid());
        if (a != null) {
            if(a.getPBBookname()!=null&&!a.getPBBookname().equals(""))
            {
                apBaseEntity.setPBBookname(a.getPBBookname());
            }
            else
            {
                apBaseEntity.setPBBookname(apBaseEntity.getPBName()+"项目申请书");
            }

        }
        else
        {
            apBaseEntity.setPBBookname(apBaseEntity.getPBName()+"项目申请书");
        }

        if (a != null && a.getPBDatastate().equals("删除")) {
            return R.error("该项目已删除");
        }
        if (a != null && (a.getPOperationState().equals("104") )) {
            return R.error("该项目已提交审核，不能再进行修改操作");
        }
        if (a != null && (a.getPOperationState().equals("106"))) {
            return R.error("该项目已归档，不能再进行修改操作");
        }
        if(a != null&&( a.getPOperationState().equals("101")||a.getPOperationState().equals("102")))
        {
            apBaseEntity.setPOperationState("103");
        }
        if(a == null)
        {
            apBaseEntity.setPOperationState("103");
        }
        List<APCostEntity> apCostEntities = JSON.parseArray(map.get("apCostEntities").toString(), APCostEntity.class);
        List<APCriteriaEntity> apCriteriaEntities = JSON.parseArray(map.get("apCriteriaEntities").toString(), APCriteriaEntity.class);
        List<APEstimateEntity> apEstimateEntities = JSON.parseArray(map.get("apEstimateEntities").toString(), APEstimateEntity.class);
        List<APOtherEntity> apOtherEntities = JSON.parseArray(map.get("apOtherEntities").toString(), APOtherEntity.class);
        List<APRangeEntity> apRangeEntities = JSON.parseArray(map.get("apRangeEntities").toString(), APRangeEntity.class);
        List<APWireEntity> apWireEntities = JSON.parseArray(map.get("apWireEntities").toString(), APWireEntity.class);
        apBaseService.insertOrReplaceOrDelete(apBaseEntity);
        apCostService.insertOrReplaceOrDelete(apCostEntities);
        apCriteriaService.insertOrReplaceOrDelete(apCriteriaEntities);
        apEstimateService.insertOrReplaceOrDelete(apEstimateEntities);
        apOtherService.insertOrReplaceOrDelete(apOtherEntities);
        apRangeService.insertOrReplaceOrDelete(apRangeEntities);
        apWireService.insertOrReplaceOrDelete(apWireEntities);
        return R.ok();
    }

    /**
     * 更新项目（点位信息）
     *
     * @return
     */
    @RequestMapping("/projectPoint")
    public R updateProjectPoint(@RequestParam Map<String, Object> map) {
        List<DCrossCostEntity> dCrossCostEntities = JSON.parseArray(map.get("dCrossCostEntities").toString(), DCrossCostEntity.class);
        List<DDrawLineEntity> dDrawLineEntities = JSON.parseArray(map.get("dDrawLineEntities").toString(), DDrawLineEntity.class);
        List<DPointEntity> dPointEntities = JSON.parseArray(map.get("dPointEntities").toString(), DPointEntity.class);
        if (dPointEntities.size() > 0) {
            APBaseEntity a = apBaseService.queryObject(dPointEntities.get(0).getPGuid());
            a.setPBLatitude(dPointEntities.get(0).getPPInputlatitude());
            a.setPBLongitude(dPointEntities.get(0).getPPInputlongitude());
            apBaseService.update(a);
        }
        dCrossCostService.insertOrReplaceOrDelete(dCrossCostEntities);
        dDrawLineService.insertOrReplaceOrDelete(dDrawLineEntities);
        dPointService.insertOrReplaceOrDelete(dPointEntities);
        return R.ok();
    }

    /**
     * 更新项目（设备材料信息）
     *
     * @return
     */
    @RequestMapping("/projectDeviceAndMaterial")
    public R updateProjectDeviceAndMaterial(@RequestParam Map<String, Object> map) {
        List<DDeviceBuyEntity> dDeviceBuyEntities = JSON.parseArray(map.get("dDeviceBuyEntities").toString(), DDeviceBuyEntity.class);
        List<DDeviceOldEntity> dDeviceOldEntities = JSON.parseArray(map.get("dDeviceOldEntities").toString(), DDeviceOldEntity.class);
        List<DMaterialBuyEntity> dMaterialBuyEntities = JSON.parseArray(map.get("dMaterialBuyEntities").toString(), DMaterialBuyEntity.class);
        List<DMaterialOldEntity> dMaterialOldEntities = JSON.parseArray(map.get("dMaterialOldEntities").toString(), DMaterialOldEntity.class);
        dDeviceBuyService.insertOrReplaceOrDelete(dDeviceBuyEntities);
        dDeviceOldService.insertOrReplaceOrDelete(dDeviceOldEntities);
        dMaterialBuyService.insertOrReplaceOrDelete(dMaterialBuyEntities);
        dMaterialOldService.insertOrReplaceOrDelete(dMaterialOldEntities);
        return R.ok();
    }

    /**
     * 更新项目（工程量）
     *
     * @return
     */
    @RequestMapping("/projectCross")
    public R updateProjectCross(@RequestParam Map<String, Object> map) {
        List<DOtherCostEntity> dOtherCostEntities = JSON.parseArray(map.get("dOtherCostEntities").toString(), DOtherCostEntity.class);
        List<DMaterialCostEntity> dMaterialCostEntities = JSON.parseArray(map.get("dMaterialCostEntities").toString(), DMaterialCostEntity.class);
        List<DWorkContentEntity> dWorkContentEntities = JSON.parseArray(map.get("dWorkContentEntities").toString(), DWorkContentEntity.class);
        dOtherCostService.insertOrReplaceOrDelete(dOtherCostEntities);
        dMaterialCostService.insertOrReplaceOrDelete(dMaterialCostEntities);
        dWorkContentService.insertOrReplaceOrDelete(dWorkContentEntities);
        return R.ok();
    }

    /**
     * 获取项目列表
     *
     * @return
     */
    @RequestMapping("/getProjectInfo")
    public R getProjectInfo(@RequestParam Map<String, Object> map) {


        //  登录用户信息
        SysUserEntity sysUserEntity = shiroService.queryByToken(map.get("token").toString());
        //  本地项目id
        String[] locationGuids = new String[]{};
        if(map.get("locationGuids") != null && !map.get("locationGuids").equals("")){
            locationGuids = map.get("locationGuids").toString().split(",");
        }

        //  返回的项目列表
        APBaseListAllEntity alaEnt = new APBaseListAllEntity();
        List<APBaseListAllEntity> aPBaseListAllEntities = new ArrayList<>();
        //  本地项目状态
        List<APBaseLocationEntity> aleList = new ArrayList<>();
        //  服务器项目
        List<APBaseEntity> apBaseEntities = new ArrayList<>();
        Map<String, Object> qmap = new HashMap<>();
        StringBuffer stringBuffer = new StringBuffer();
        String filter = "";
        int totalProject = 0;                     //  项目数量
        if(locationGuids.length > 0 ){

            //  本地项目状态
            for (String locationGuid : locationGuids) {
                APBaseLocationEntity ale = new APBaseLocationEntity();
                APBaseEntity apBaseEntity = apBaseService.queryObject(locationGuid);
                if(apBaseEntity!=null)
                {
                    ale.setGuid(locationGuid);
                    ale.setPName(apBaseEntity.getPBName());
                    ale.setPState(apBaseEntity.getPOperationState());
                    aleList.add(ale);
                }
//                String pbState = bDataDictionaryService.selectvaluebykey("operation_state",apBaseEntity.getPOperationState());

            }


            //  本地有项目的情况，需要排除掉已有的项目
            filter = "p_b_datastate !='删除' and (p_operation_state = '101' or p_operation_state = '102' or p_operation_state = '103'or p_operation_state = '105') " +
                    "and FIND_IN_SET('" + sysUserEntity.getUserId() + "',p_b_collectid) and guid not in ( ";
            for (String locationGuid : locationGuids) {
                stringBuffer.append("'"+locationGuid+"'");
                stringBuffer.append(",");
            }
            String s2 = stringBuffer.substring(0,stringBuffer.lastIndexOf(","));
            filter += s2 +" ) ";

        }else{  //  本地没有项目的情况，查询所属用户的所有项目
            filter = "p_b_datastate !='删除' and (p_operation_state = '101' or p_operation_state = '102' or p_operation_state = '103'or p_operation_state = '105') " +
                    "and FIND_IN_SET('" + sysUserEntity.getUserId() + "',p_b_collectid)";
        }

        qmap.put("filterSql", filter);
        apBaseEntities = apBaseService.queryList(qmap);
        totalProject = apBaseEntities.size();
        alaEnt.setTotalProject(totalProject);
        alaEnt.setAPBaseLocationEntities(aleList);
        alaEnt.setAPBaseEntities(apBaseEntities);
        aPBaseListAllEntities.add(alaEnt);


        return R.ok().put("aPBaseListAllEntities", aPBaseListAllEntities);



//        Map<String, Object> qmap = new HashMap<>();
//        SysUserEntity sysUserEntity = shiroService.queryByToken(map.get("token").toString());
//        String filter = "p_b_datastate !='删除' and (p_b_state = '10' or p_b_state = '20') and FIND_IN_SET('" + sysUserEntity.getUserId() + "',p_b_collectid)";
//        qmap.put("filterSql", filter);
//        List<APBaseEntity> apBaseEntities = apBaseService.queryList(qmap);
//        return R.ok().put("apBaseEntities", apBaseEntities);


    }


    /**
     *  获取已归档项目列表
     *
     * @param map
     * @return
     */
    @RequestMapping("/getPBState30List")
    public R getPBState30List(@RequestParam Map<String, Object> map){

        SysUserEntity sysUserEntity = shiroService.queryByToken(map.get("token").toString());
        Map<String, Object> qmap = new HashMap<>();
        String filter = "p_b_datastate !='删除' and (p_operation_state = '106') " +
                "and FIND_IN_SET('" + sysUserEntity.getUserId() + "',p_b_collectid)  ";
        qmap.put("filterSql", filter);
        List<APBaseEntity> apBaseEntityList = apBaseService.queryList(qmap);


        return R.ok().put("apBaseEntityList",apBaseEntityList);
    }



    /**
     * 更新隐患
     *
     * @return
     */
    @RequestMapping("/problemUpdate")
    public R updateProblem(@RequestParam Map<String, Object> map) {
        EPBaseEntity epBaseEntity = JSON.parseObject(map.get("epBaseEntity").toString(), EPBaseEntity.class);
        EPBaseEntity entity = epBaseService.queryObject(epBaseEntity.getGuid());
        if (entity != null && entity.getPBDatastate().equals("删除")) {
            return R.error("该项目已删除");
        }
        if (entity != null && entity.getPBState().equals("30")) {
            return R.error("该项目已归档");
        }
        List<ECrossCostEntity> eCrossCostEntities = JSON.parseArray(map.get("eCrossCostEntities").toString(), ECrossCostEntity.class);
        List<EDrawLineEntity> eDrawLineEntities = JSON.parseArray(map.get("eDrawLineEntities").toString(), EDrawLineEntity.class);
        List<EPointEntity> ePointEntities = JSON.parseArray(map.get("ePointEntities").toString(), EPointEntity.class);
        List<EPRangeEntity> epRangeEntities = JSON.parseArray(map.get("epRangeEntities").toString(), EPRangeEntity.class);
        List<EPWireEntity> epWireEntities = JSON.parseArray(map.get("epWireEntities").toString(), EPWireEntity.class);
        List<EProblemexistEntity> eProblemexistEntities = JSON.parseArray(map.get("eProblemexistEntities").toString(), EProblemexistEntity.class);
        List<EDeviceOldEntity> eDeviceOldEntities = JSON.parseArray(map.get("eDeviceOldEntities").toString(), EDeviceOldEntity.class);
        List<EDeviceBuyEntity> eDeviceBuyEntities = JSON.parseArray(map.get("eDeviceBuyEntities").toString(), EDeviceBuyEntity.class);
        List<EMaterialOldEntity> eMaterialOldEntities = JSON.parseArray(map.get("eMaterialOldEntities").toString(), EMaterialOldEntity.class);
        List<EMaterialBuyEntity> eMaterialBuyEntities = JSON.parseArray(map.get("eMaterialBuyEntities").toString(), EMaterialBuyEntity.class);
        List<EWorkContentEntity> eWorkContentEntities = JSON.parseArray(map.get("eWorkContentEntities").toString(), EWorkContentEntity.class);
        if (ePointEntities.size() > 0) {
            entity.setPBLatitude(ePointEntities.get(0).getPPInputlatitude());
            entity.setPBLongitude(ePointEntities.get(0).getPPInputlongitude());
        }
        epBaseService.insertOrReplaceOrDelete(epBaseEntity);
        eCrossCostService.insertOrReplaceOrDelete(eCrossCostEntities);
        eDrawLineService.insertOrReplaceOrDelete(eDrawLineEntities);
        ePointService.insertOrReplaceOrDelete(ePointEntities);
        epRangeService.insertOrReplaceOrDelete(epRangeEntities);
        epWireService.insertOrReplaceOrDelete(epWireEntities);
        eProblemexistService.insertOrReplaceOrDelete(eProblemexistEntities);
        eDeviceOldService.insertOrReplaceOrDelete(eDeviceOldEntities);
        eDeviceBuyService.insertOrReplaceOrDelete(eDeviceBuyEntities);
        eMaterialOldService.insertOrReplaceOrDelete(eMaterialOldEntities);
        eMaterialBuyService.insertOrReplaceOrDelete(eMaterialBuyEntities);
        eWorkContentService.insertOrReplaceOrDelete(eWorkContentEntities);
        return R.ok();
    }


    /**
     * 获取隐患信息
     *
     * @return
     */
    @RequestMapping("/getProblemInfo")
    public R getProblemInfo(@RequestParam Map<String, Object> map) {
        Map<String, Object> qmap = new HashMap<>();
        SysUserEntity sysUserEntity = shiroService.queryByToken(map.get("token").toString());
        String filter = "p_b_datastate !='删除'  and FIND_IN_SET('" + sysUserEntity.getUserId() + "',p_b_collectid) and p_b_state = '10'";
        qmap.put("filterSql", filter);
        List<EPBaseEntity> epBaseEntities = epBaseService.queryList(qmap);
        return R.ok().put("epBaseEntities", epBaseEntities);
    }


    /**
     * 下载项目
     *
     * @return
     */
    @RequestMapping("/downProject")
    public R downProject(String uuid) {
        Map<String, Object> map = new HashMap<>();
        String filter = "p_guid = '" + uuid + "'";
        map.put("filterSql", filter);
        APBaseEntity apBaseEntity = apBaseService.queryObject(uuid);
           if( apBaseEntity.getPOperationState().equals("101"))
           {
               apBaseEntity.setPOperationState("102");
               apBaseService.update(apBaseEntity);
           }

        List<APCostEntity> apCostEntities = apCostService.queryList(map);
        List<APCriteriaEntity> apCriteriaEntities = apCriteriaService.queryList(map);
        List<APEstimateEntity> apEstimateEntities = apEstimateService.queryList(map);
        List<APOtherEntity> apOtherEntities = apOtherService.queryList(map);
        List<APRangeEntity> apRangeEntities = apRangeService.queryList(map);
        List<APWireEntity> apWireEntities = apWireService.queryList(map);
        List<DCrossCostEntity> dCrossCostEntities = dCrossCostService.queryList(map);
        List<DDeviceBuyEntity> dDeviceBuyEntities = dDeviceBuyService.queryList(map);
        List<DDeviceOldEntity> dDeviceOldEntities = dDeviceOldService.queryList(map);
        List<DDrawLineEntity> dDrawLineEntities = dDrawLineService.queryList(map);
        List<DMaterialBuyEntity> dMaterialBuyEntities = dMaterialBuyService.queryList(map);
        List<DMaterialCostEntity> dMaterialCostEntities = dMaterialCostService.queryList(map);
        List<DMaterialOldEntity> dMaterialOldEntities = dMaterialOldService.queryList(map);
        List<DOtherCostEntity> dOtherCostEntities = dOtherCostService.queryList(map);
        List<DPointEntity> dPointEntities = dPointService.queryList(map);
        List<DWorkContentEntity> dWorkContentEntities = dWorkContentService.queryList(map);
        filter += "and p_status = '储备'";
        map.put("filterSql", filter);
        List<DPictureEntity> dPictureEntities = dPictureService.queryList(map);
        return R.ok()
                .put("apBaseEntity", apBaseEntity)
                .put("apCostEntities", apCostEntities)
                .put("apCriteriaEntities", apCriteriaEntities)
                .put("apEstimateEntities", apEstimateEntities)
                .put("apOtherEntities", apOtherEntities)
                .put("apRangeEntities", apRangeEntities)
                .put("apWireEntities", apWireEntities)
                .put("dCrossCostEntities", dCrossCostEntities)
                .put("dDeviceBuyEntities", dDeviceBuyEntities)
                .put("dDeviceOldEntities", dDeviceOldEntities)
                .put("dDrawLineEntities", dDrawLineEntities)
                .put("dMaterialBuyEntities", dMaterialBuyEntities)
                .put("dMaterialCostEntities", dMaterialCostEntities)
                .put("dMaterialOldEntities", dMaterialOldEntities)
                .put("dOtherCostEntities", dOtherCostEntities)
                .put("dPictureEntities", dPictureEntities)
                .put("dPointEntities", dPointEntities)
                .put("dWorkContentEntities", dWorkContentEntities)
                ;
    }

    /**
     * 下载隐患
     *
     * @return
     */
    @RequestMapping("/downProblem")
    public R downProblem(String uuid) {
        Map<String, Object> map = new HashMap<>();
        String filter = "p_guid = '" + uuid + "'";
        map.put("filterSql", filter);
        List<ECrossCostEntity> eCrossCostEntities = eCrossCostService.queryList(map);
        List<EDrawLineEntity> eDrawLineEntities = eDrawLineService.queryList(map);
        List<EPointEntity> ePointEntities = ePointService.queryList(map);
        List<EPRangeEntity> epRangeEntities = epRangeService.queryList(map);
        List<EPWireEntity> epWireEntities = epWireService.queryList(map);
        List<EDeviceBuyEntity> eDeviceBuyEntities = eDeviceBuyService.queryList(map);
        List<EDeviceOldEntity> eDeviceOldEntities = eDeviceOldService.queryList(map);
        List<EMaterialBuyEntity> eMaterialBuyEntities = eMaterialBuyService.queryList(map);
        List<EMaterialOldEntity> eMaterialOldEntities = eMaterialOldService.queryList(map);
        List<EWorkContentEntity> eWorkContentEntities = eWorkContentService.queryList(map);
        EPBaseEntity epBaseEntity = epBaseService.queryObject(uuid);
        filter = "projectguid = '" + uuid + "'";
        map.put("filterSql", filter);
        List<EProblemexistEntity> eProblemexistEntities = eProblemexistService.queryList(map);
        filter = "p_guid = '" + uuid + "'";
        filter += "and p_status = '验收'";
        map.put("filterSql", filter);
        List<DPictureEntity> dPictureEntities = dPictureService.queryList(map);
        return R.ok()
                .put("eCrossCostEntities", eCrossCostEntities)
                .put("eDrawLineEntities", eDrawLineEntities)
                .put("ePointEntities", ePointEntities)
                .put("epRangeEntities", epRangeEntities)
                .put("epWireEntities", epWireEntities)
                .put("epBaseEntity", epBaseEntity)
                .put("eProblemexistEntities", eProblemexistEntities)
                .put("dPictureEntities", dPictureEntities)
                .put("eDeviceBuyEntities", eDeviceBuyEntities)
                .put("eDeviceOldEntities", eDeviceOldEntities)
                .put("eMaterialBuyEntities", eMaterialBuyEntities)
                .put("eMaterialOldEntities", eMaterialOldEntities)
                .put("eWorkContentEntities", eWorkContentEntities)
                ;
    }

    @RequestMapping("/updateMaterialModel")
    public R updateMaterialModel(@RequestParam Map<String, Object> map) {
        List<DMaterialModelEntity> dMaterialModelEntities = JSON.parseArray(map.get("dMaterialModelEntities").toString(), DMaterialModelEntity.class);
        List<DMaterialModelItemEntity> dMaterialModelItemEntities = JSON.parseArray(map.get("dMaterialModelItemEntities").toString(), DMaterialModelItemEntity.class);
        dMaterialModelService.insertOrReplaceOrDelete(dMaterialModelEntities);
        dMaterialModelItemService.insertOrReplaceOrDelete(dMaterialModelItemEntities);
        SysUserEntity sysUserEntity = shiroService.queryByToken(map.get("token").toString());
        Map<String, Object> q = new HashMap<>();
        String filter = "d_material_model.range =  1 or create_id = " + sysUserEntity.getUserId();
        q.put("filterSql", filter);
        List<DMaterialModelEntity> dMaterialModelEntities1 = dMaterialModelService.queryList(q);
        List<DMaterialModelItemEntity> entities = new ArrayList<>();
        for (DMaterialModelEntity d : dMaterialModelEntities1) {
            filter = "model_guid = '" + d.getGuid() + "'";
            q.put("filterSql", filter);
            List<DMaterialModelItemEntity> entities1 = dMaterialModelItemService.queryList(q);
            entities.addAll(entities1);
        }
        return R.ok().put("dMaterialModelEntities", dMaterialModelEntities1)
                .put("dMaterialModelItemEntities", entities);
    }

}
